package com.example.qiniutest.aliyun.ocr.service.impl;

import com.aliyun.ocr20191230.Client;
import com.aliyun.ocr20191230.models.RecognizeVATInvoiceRequest;
import com.aliyun.ocr20191230.models.RecognizeVATInvoiceResponse;
import com.aliyun.ocr20191230.models.RecognizeVATInvoiceResponseBody;
import com.aliyun.teaopenapi.models.Config;
import com.example.qiniutest.aliyun.ocr.config.OcrConfig;
import com.example.qiniutest.aliyun.ocr.service.IOcrService;
import com.example.qiniutest.aliyun.oss.config.ALiYunOSSConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : xpp011
 * @create 2021/12/24 15:50
 */
@Service
public class OcrServiceImpl implements IOcrService {


    @Autowired
    private ALiYunOSSConfig aLiYunOSSConfig;

    @Autowired
    private OcrConfig ocrConfig;

    private Client createClient() throws Exception {
        Config config=new Config()
                .setAccessKeyId(aLiYunOSSConfig.getAccessKeyId())
                .setAccessKeySecret(aLiYunOSSConfig.getAccessKeySecret())
                .setEndpoint(ocrConfig.getEndpoint());
        return new Client(config);
    }

    @Override
    public RecognizeVATInvoiceResponseBody parse(String downUrl, String suffix) {
        RecognizeVATInvoiceResponseBody res = null;
        try {
            Client client = createClient();
            RecognizeVATInvoiceRequest request = new RecognizeVATInvoiceRequest()
                    .setFileURL(downUrl)
                    .setFileType(suffix);
            RecognizeVATInvoiceResponse response = client.recognizeVATInvoice(request);
            res = response.getBody();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }
}
